import 'package:flutter/material.dart';
import 'package:get/get.dart';

import 'logic.dart';

class MerchantHomePage extends StatelessWidget {
  final MerchantHomeLogic logic = Get.put(MerchantHomeLogic());

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('商家管理'),
        backgroundColor: Color(0xFFBE9650),
        foregroundColor: Colors.white,
        actions: [
          IconButton(
            icon: Icon(Icons.refresh),
            onPressed: logic.loadStatistics,
          ),
        ],
      ),
      body: Obx(() {
        if (logic.isLoading.value) {
          return Center(child: CircularProgressIndicator());
        }
        
        return SingleChildScrollView(
          padding: EdgeInsets.all(16),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              // 欢迎信息
              Card(
                elevation: 2,
                child: Padding(
                  padding: EdgeInsets.all(16),
                  child: Row(
                    children: [
                      CircleAvatar(
                        backgroundColor: Color(0xFFBE9650),
                        radius: 30,
                        child: Icon(Icons.store, size: 30, color: Colors.white),
                      ),
                      SizedBox(width: 16),
                      Column(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: [
                          Text(
                            '欢迎回来，商家',
                            style: TextStyle(
                              fontSize: 20,
                              fontWeight: FontWeight.bold,
                            ),
                          ),
                          SizedBox(height: 4),
                          Text(
                            '今天是个美好的一天',
                            style: TextStyle(color: Colors.grey[600]),
                          ),
                        ],
                      ),
                    ],
                  ),
                ),
              ),
              
              SizedBox(height: 24),
              
              // 统计卡片
              Text(
                '概览',
                style: TextStyle(
                  fontSize: 18,
                  fontWeight: FontWeight.bold,
                ),
              ),
              SizedBox(height: 12),
              
              Row(
                children: [
                  // 待处理订单
                  Expanded(
                    child: _buildStatCard(
                      icon: Icons.pending_actions,
                      iconColor: Colors.orange,
                      title: '待处理订单',
                      value: logic.pendingOrderCount.toString(),
                      onTap: logic.goToOrderManagement,
                    ),
                  ),
                  SizedBox(width: 16),
                  // 总订单数
                  Expanded(
                    child: _buildStatCard(
                      icon: Icons.receipt_long,
                      iconColor: Colors.blue,
                      title: '总订单数',
                      value: logic.totalOrderCount.toString(),
                      onTap: logic.goToOrderManagement,
                    ),
                  ),
                ],
              ),
              SizedBox(height: 16),
              Row(
                children: [
                  // 菜品数量
                  Expanded(
                    child: _buildStatCard(
                      icon: Icons.restaurant_menu,
                      iconColor: Colors.green,
                      title: '菜品数量',
                      value: logic.totalFoodCount.toString(),
                      onTap: logic.goToFoodManagement,
                    ),
                  ),
                  SizedBox(width: 16),
                  // 占位
                  Expanded(child: SizedBox()),
                ],
              ),
              
              SizedBox(height: 24),
              
              // 快捷操作
              Text(
                '快捷操作',
                style: TextStyle(
                  fontSize: 18,
                  fontWeight: FontWeight.bold,
                ),
              ),
              SizedBox(height: 12),
              
              // 订单管理
              _buildActionCard(
                icon: Icons.receipt,
                title: '订单管理',
                description: '查看和管理所有订单',
                onTap: logic.goToOrderManagement,
              ),
              
              SizedBox(height: 16),
              
              // 菜品管理
              _buildActionCard(
                icon: Icons.restaurant_menu,
                title: '菜品管理',
                description: '添加、编辑和删除菜品',
                onTap: logic.goToFoodManagement,
              ),
            ],
          ),
        );
      }),
    );
  }
  
  // 统计卡片
  Widget _buildStatCard({
    required IconData icon,
    required Color iconColor,
    required String title,
    required String value,
    required VoidCallback onTap,
  }) {
    return Card(
      elevation: 2,
      child: InkWell(
        onTap: onTap,
        borderRadius: BorderRadius.circular(4),
        child: Padding(
          padding: EdgeInsets.all(16),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Icon(icon, color: iconColor, size: 28),
              SizedBox(height: 12),
              Text(
                title,
                style: TextStyle(
                  color: Colors.grey[600],
                  fontSize: 14,
                ),
              ),
              SizedBox(height: 4),
              Text(
                value,
                style: TextStyle(
                  fontSize: 24,
                  fontWeight: FontWeight.bold,
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }
  
  // 操作卡片
  Widget _buildActionCard({
    required IconData icon,
    required String title,
    required String description,
    required VoidCallback onTap,
  }) {
    return Card(
      elevation: 2,
      child: InkWell(
        onTap: onTap,
        borderRadius: BorderRadius.circular(4),
        child: Padding(
          padding: EdgeInsets.all(16),
          child: Row(
            children: [
              Container(
                padding: EdgeInsets.all(12),
                decoration: BoxDecoration(
                  color: Color(0xFFBE9650).withOpacity(0.1),
                  borderRadius: BorderRadius.circular(8),
                ),
                child: Icon(icon, color: Color(0xFFBE9650), size: 24),
              ),
              SizedBox(width: 16),
              Expanded(
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Text(
                      title,
                      style: TextStyle(
                        fontSize: 16,
                        fontWeight: FontWeight.bold,
                      ),
                    ),
                    SizedBox(height: 4),
                    Text(
                      description,
                      style: TextStyle(color: Colors.grey[600], fontSize: 14),
                    ),
                  ],
                ),
              ),
              Icon(Icons.arrow_forward_ios, color: Colors.grey, size: 16),
            ],
          ),
        ),
      ),
    );
  }
}
